package com.yvtian.admin.controller;

import com.yvtian.admin.dto.ReqPermission;
import com.yvtian.admin.service.PermissionService;
import com.yvtian.admin.vo.Permission;
import com.yvtian.annotation.Log;
import com.yvtian.annotation.PreAuthorize;
import com.yvtian.entity.Result;
import com.yvtian.entity.StatusCode;
import com.yvtian.enums.OperatorType;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping(value = "/api/admin/per")
public class PermissionController {

    @Autowired
    private PermissionService permissionService;

    /**
     * 更新权限
     * @param permissionId 权限Id
     * @param map 要更新的列
     */
    @Log(value = "更新权限",isSave = true,operType = OperatorType.UPDATE)
    @PreAuthorize(hasPermission = "per:update")
    @RequestMapping(value = "/update/{permissionId}", method = RequestMethod.POST)
    public Result updatePermission(@PathVariable("permissionId") Integer permissionId,@RequestBody Map<String, Object> map) {
        if (permissionId == null) {
            return new Result(false, "id不能为空", StatusCode.PARAMS_IN_ERROR);
        }
        permissionService.updatePer(permissionId,map);
        return new Result(true, "修改成功", StatusCode.OK);
    }

    /**
     * 批量删除权限
     * @param map ids：权限id的集合
     */
    @Log(value = "批量删除权限",isSave = true,operType = OperatorType.DELETE)
    @PreAuthorize(hasPermission = "per:del")
    @RequestMapping(value = "/del", method = RequestMethod.POST)
    public Result delPermission(@RequestBody Map<String, ArrayList<Integer>> map) {
        ArrayList<Integer> ids = map.get("ids");
        if (null == ids || ids.size() == 0) {
            return new Result(false, "权限id不能为空", StatusCode.PARAMS_IN_ERROR);
        }
        permissionService.delPer(ids);
        return new Result(true, "删除成功", StatusCode.OK);
    }

    /**
     * 添加权限
     */
    @Log(value = "新增权限",isSave = true,operType = OperatorType.INSERT)
    @PreAuthorize(hasPermission = "per:add")
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public Result addPermission(@Valid @RequestBody ReqPermission permission) {
        if (permission.getParentId() == null) {
            return new Result(false, "父Id不能为空", StatusCode.PARAMS_IN_ERROR);
        }
        permissionService.addPer(permission);
        return new Result(true, "添加成功", StatusCode.OK);
    }

    /**
     * 获取所有权限
     */
    @Log("获取所有权限")
    @PreAuthorize(hasPermission = "per:find")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public Result findAll() {
        return new Result(true, "查询成功", StatusCode.OK, permissionService.findAll());
    }

    /**
     * 根据父Id查询权限
     * @param parentId 父权限Id
     */
    @Log("根据父Id查询权限")
    @PreAuthorize(hasPermission = "per:list")
    @RequestMapping(value = "/list/{parentId}", method = RequestMethod.GET)
    public Result findByPage(@PathVariable("parentId") Integer parentId) {
        List<Permission> permissions = permissionService.findByParentId(parentId);
        log.info("权限列表出参->{}", permissions.toString());
        return new Result(true, "查询成功", StatusCode.OK, permissions);
    }

    /**
     * 查询角色的权限
     *
     * @param roleId 角色Id
     */
    @Log("查询角色权限")
    @PreAuthorize(hasPermission = "per:find")
    @RequestMapping(value = "/role/{roleId}", method = RequestMethod.GET)
    public Result findPermissionByRoleId(@PathVariable Integer roleId) {
        List<Permission> permissions = permissionService.findByRoleId(roleId);
        log.info("权限列表出参->{}", permissions.toString());
        return new Result(true, "查询成功", StatusCode.OK, permissions);
    }

}
